﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using Ysb.Components;
public partial class Admin_AddChengben : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
   
        if (!Page.IsPostBack)
        {
            if (!AdminCheck.Check(this, 1))
            {
                Response.Redirect(AdminCheck.RedirectPage);
                return;
            }
            BindDAll();
            BindDll();
        }

    }
    private void BindDAll()
    {
        selectmudidi.DataSource = new PlaceService().GetEnd();
        selectmudidi.DataTextField = "PlaceName";
     //   selectmudidi.DataValueField = "id";
        selectmudidi.DataBind();

        selectqiyundi.DataSource = new PlaceService().GetStart();
        selectqiyundi.DataTextField = "PlaceName";
       // selectqiyundi.DataValueField = "id";
        selectqiyundi.DataBind();
    }
    private void BindDll()
    {

        if (Request.QueryString["id"] != null)
        {
            DataTable dt = new PublicSqlService().GetData("*", "Tb_Cost", "id=" + Request.QueryString["id"].ToString());

            if (dt.Rows.Count != 1)
            {
                return;
            }
            fightpay.Value = dt.Rows[0]["FigthPay"].ToString();
            gupneipay.Value = dt.Rows[0]["DomesticPay"].ToString();
            taxtxt.Value = dt.Rows[0]["TaxPay"].ToString();

            otherpay.Value = dt.Rows[0]["OtherPay"].ToString();

            weightkg.Value = dt.Rows[0]["weightKG"].ToString();
            weightlb.Value = dt.Rows[0]["weightLb"].ToString();

            shourutxt.Value = dt.Rows[0]["Income"].ToString();


            figthno.Value = dt.Rows[0]["FigthNo"].ToString();




            datetxt.Value = dt.Rows[0]["AccountingDate"].ToString();
            selectqiyundi.Text = dt.Rows[0]["QiYunDi"].ToString();

            selectmudidi.Text = dt.Rows[0]["MuDiDi"].ToString();





        }

    }
    protected void savebtn_Click(object sender, ImageClickEventArgs e)
    {

        //Response.Write(selectqiyundi.SelectedValue);
        //return;
        if (InsertOk())
        {
            Response.Redirect("Chengben.aspx");

        }
        else
        {
            Tools.OpenWindow(this, "保存失败");
        }
    }
    protected void saveandadd_Click(object sender, ImageClickEventArgs e)
    {


        if (InsertOk())
        {
            Tools.OpenWindow(this, "保存成功！请继续录入", "AddChengben.aspx");

        }
        else
        {
            Tools.OpenWindow(this, "保存失败");
        }
    }

    private bool InsertOk()
    {
        Tools t = new Tools();

        Hashtable hs = new Hashtable();
        decimal sr = t.StringToDecimalDif(shourutxt.Value);

        decimal f = t.StringToDecimalDif(fightpay.Value);
        decimal gj = t.StringToDecimalDif(gupneipay.Value);
        decimal tax = t.StringToDecimalDif(taxtxt.Value);
        decimal other = t.StringToDecimalDif(otherpay.Value);
        hs.Add("QiYunDi", selectqiyundi.SelectedItem.Text.Trim());
        hs.Add("MuDiDi", selectmudidi.SelectedItem.Text.Trim());
        hs.Add("AccountingDate", t.StringToTime(datetxt.Value));
        hs.Add("FigthNo", figthno.Value);
        hs.Add("weightKG", t.StringToDecimalDif(weightkg.Value));
        hs.Add("weightLb", t.StringToDecimalDif(weightlb.Value));
        hs.Add("Income", sr);
        hs.Add("FigthPay", f);
        hs.Add("DomesticPay", gj);
        hs.Add("TaxPay", tax); ;
        hs.Add("OtherPay", other);
        decimal zong = f + gj + tax + other;
        decimal Profit = sr - zong;


        if (t.StringToDecimalDif(weightlb.Value) == 0)
        {
            Tools.OpenWindow(this, "重量输入异常！请输入数字");
            return false;
        }
        decimal Cost = zong / t.StringToDecimalDif(weightlb.Value);




        hs.Add("Profit", Profit);
        hs.Add("Cost", Cost.ToString("0.00"));

        if (Request.QueryString["id"] == null)
        {
            //录入

            hs.Add("PostDate", DateTime.Now);
            hs.Add("PostUser", Session["uid"].ToString());


            return new PublicSqlService().Insert(hs, "Tb_Cost");
        }
        else
        {
            //编辑
            return new PublicSqlService().Update(hs, "Tb_Cost", "ID=" + Request.QueryString["id"].ToString());
        }



    }
}
